<?php
namespace Home\Controller;
use Think\Controller;
class OtherController extends Controller {
	 /**
     * 管理员列表
     */
    public function index() {
        //使用D调用视图模型
        $other = M('other');
		$pid = I('get.pid',0,'int');
		$where['pid'] = array('eq',$pid);
        $data = $other->where($where)->order('id desc')->select();
		$this->assign('pid',$pid);
        $this->assign('data',$data);
        $this->display();
    }
    
    /**
     * 新增管理员
     */
    public function add() {
        if(IS_POST) {
            $data['pid'] = I('post.pid',0,'int');
            $data['name'] = I('post.name','','addslashes');
            //通过D实例化模型类
            $other = D('Other');
            if($other->create()) {
                if($other->add($data)) {
                    $this->success('新增成功',U('Other/index?pid='.$data['pid']));
                }else {
                    $this->error('新增失败',U('Other/index?pid='.$data['pid']));
                }
            }else {
                $this->error($other->getError());
            }
        }else {
            //添加管理员之前 查询出已有的角色
			$pid = I('get.pid',0,'int');
            $other = M('other');
			if($pid != 0){
				$other_data = $other->where('id='.$pid)->find();	
				$this->assign('other_data',$other_data);
			}
            $this->display();
        }
    }
    
    /**
     * 编辑管理员
     */
    public function edit() {
        //通过D实例化模型类
        $other = D('Other');
        if(IS_POST) {
            $id = I('post.id',0,'int');
            $data['pid'] = I('post.pid',0,'int');
            $data['name'] = I('post.name','','addslashes');
			//var_dump($_POST);exit;
            if($other->create()) {
                if($other->where('id='.$id)->save($data) !== false) {
                    $this->success('编辑成功',U('Other/index?pid='.$data['pid']));
                }else {
                    $this->error('编辑失败',U('Other/index?pid='.$data['pid']));
                }
            }else {
                $this->error($other->getError());
            }
        }else {
            //获取要编辑的数据
            $id = I('get.id',1,'int');
            $data = $other->where('id='.$id)->find();
            //添加管理员之前 查询出已有的角色
            if($data['pid'] != 0){
            	$other_data = $other->where('id='.$data['pid'])->find();	
            	$this->assign('other_data',$other_data);
            }
            $this->assign('data',$data);
            $this->display();
        }
    }
    
    /**
     * 删除管理员
     */
    public function del() {
        if(IS_POST) {
            $id = I('post.id',0,'int');
			//echo $id;exit;
            //实例化管理员表
            $other = M('other');
			if($other->where('id='.$id.' and pid !=0')->delete()) {
					echo 1;exit;
			}else {
					echo 0;exit;
			}  
        }else {
            echo -1;exit;
        }
    }
}